#!usr/bin/python
# -*= coding: UTF-8 -*-
'''
描述符就是将某种特殊类型的类的实例指派给另一个类的属性
__get__(self, instance, owner)
-用户访问属性，它返回属性的值
__set__(self, instance, value)
-将在属性分配操作中调用，不返回任何内容
__delete__(self, instance)
-控制删除操作，不返回任何内容
'''
class MyDescriptor:
    def __get__(self, instance, owner):
        print('getting...', self, instance, owner)

    def __set__(self, instance, value):
        print('setting...', self, instance, value)

    def __delete__(self, instance):
        print('deleting', self, instance)

class Test:
    x = MyDescriptor()

test = Test()
print(test.x)
print(Test)

test.x = 'X-man'
print('text.x', test.x)

del test.x
print('delete done')



class MyProperty:
    def __init__(self, fget = None, fset = None, fdel = None):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel

    def __get__(self, instance, owner):
        return self.fget(instance)  #拥有者的实例对象

    def __set__(self, instance, value):
        self.fset(instance, value)

    def __del__(self, instance):
        self.fdel(instance)

class C:
    def __init__(self):
        self._x = None

    def getX(self):
        return self._x

    def setX(self, value):
        self._x = value

    def delX(self):
        del self._x

    x = MyProperty(getX, setX, delX)

c = C()
c.x = 'X-man'
print(c.x)
#del c.x 报错






